#include "localfilemanager.h"
#include <QDir>
#include <QDateTime>
#include <QFile>
#include <QDebug>

LocalFileManager::LocalFileManager(QObject *parent) : QObject(parent)
{

}

LocalFileManager::~LocalFileManager()
{
}

QList<LocalFileManager::FileInfo> LocalFileManager::getFileList(const QString &path)
{
    QList<FileInfo> fileList;
    
    // 检查路径是否存在
    if (!QDir(path).exists()) {
        emit errorOccurred(tr("路径不存在: %1").arg(path));
        return fileList;
    }
    
    // 扫描目录获取文件列表
    scanDirectory(path, fileList);
    
    // 发送信号通知文件列表已更新
    emit fileListUpdated(fileList);
    
    return fileList;
}

LocalFileManager::FileInfo LocalFileManager::getFileInfo(const QString &filePath)
{
    QFileInfo qFileInfo(filePath);
    FileInfo info;
    
    if (qFileInfo.exists()) {
        info = convertToFileInfo(qFileInfo);
    } else {
        // 如果文件不存在，初始化空的FileInfo
        info.fileName = "";
        info.fileType = "";
        info.fileSize = 0;
        info.creationTime = "";
        info.isDirectory = false;
        info.filePath = "";
        emit errorOccurred(tr("文件不存在: %1").arg(filePath));
    }
    
    return info;
}

bool LocalFileManager::fileExists(const QString &filePath)
{
    return QFile::exists(filePath);
}

bool LocalFileManager::isDirectory(const QString &path)
{
    return QFileInfo(path).isDir();
}

bool LocalFileManager::createDirectory(const QString &path)
{
    QDir dir;
    bool success = dir.mkpath(path);
    
    if (success) {
        emit operationSuccess(tr("目录创建成功: %1").arg(path));
    } else {
        emit errorOccurred(tr("无法创建目录: %1").arg(path));
    }
    
    return success;
}

bool LocalFileManager::deleteFile(const QString &filePath)
{
    QFileInfo fileInfo(filePath);
    bool success = false;
    
    if (fileInfo.isDir()) {
        // 删除目录
        QDir dir(filePath);
        success = dir.removeRecursively();
    } else {
        // 删除文件
        success = QFile::remove(filePath);
    }
    
    if (success) {
        emit operationSuccess(tr("成功删除: %1").arg(filePath));
    } else {
        emit errorOccurred(tr("无法删除: %1").arg(filePath));
    }
    
    return success;
}

bool LocalFileManager::renameFile(const QString &oldFilePath, const QString &newFileName)
{
    // 确保newFileName只是文件名，不是完整路径
    QFileInfo oldFileInfo(oldFilePath);
    QString newFilePath = oldFileInfo.path() + QDir::separator() + newFileName;
    
    bool success = QFile::rename(oldFilePath, newFilePath);
    
    if (success) {
        emit operationSuccess(tr("成功重命名文件"));
    } else {
        emit errorOccurred(tr("无法重命名文件"));
    }
    
    return success;
}

QString LocalFileManager::formatFileSize(qint64 size)
{
    if (size < 0) return tr("无效大小");
    if (size == 0) return "0 B";
    
    // 定义大小单位
    const char* units[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
    int unitIndex = 0;
    double dSize = static_cast<double>(size);
    
    // 转换到适当的单位
    while (dSize >= 1024 && unitIndex < 6) {
        dSize /= 1024;
        unitIndex++;
    }
    
    // 格式化输出
    return QString("%1 %2").arg(QString::number(dSize, 'f', 2)).arg(units[unitIndex]);
}

QString LocalFileManager::getDefaultDirectory()
{
    // 返回用户的文档目录作为默认目录
    return QDir::homePath();
}

QString LocalFileManager::getParentDirectory(const QString &path)
{
    QDir dir(path);
    return dir.cdUp() ? dir.absolutePath() : path;
}

LocalFileManager::FileInfo LocalFileManager::convertToFileInfo(const QFileInfo &qFileInfo)
{
    FileInfo info;
    
    info.fileName = qFileInfo.fileName();
    info.filePath = qFileInfo.absoluteFilePath();
    
    if (qFileInfo.isDir()) {
        info.fileType = tr("目录");
        info.isDirectory = true;
        info.fileSize = 0; // 目录大小通常不计算
    } else {
        info.fileType = qFileInfo.suffix().isEmpty() ? tr("文件") : qFileInfo.suffix();
        info.isDirectory = false;
        info.fileSize = qFileInfo.size();
    }
    
    // 格式化创建时间
    info.creationTime = qFileInfo.birthTime().toString("yyyy-MM-dd HH:mm:ss");
    
    return info;
}

void LocalFileManager::scanDirectory(const QString &path, QList<FileInfo> &fileList)
{
    QDir dir(path);
    
    // 获取目录下所有文件和文件夹信息
    QFileInfoList qFileInfoList = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden, 
                                                   QDir::DirsFirst | QDir::Name);
    
    // 转换并添加到文件列表
    for (const QFileInfo &qFileInfo : qFileInfoList) {
        FileInfo info = convertToFileInfo(qFileInfo);
        fileList.append(info);
    }
}